package com.nsi.o2o.interceptor.shopadmin;

import com.nsi.o2o.entity.Shop;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: Luo Zhen
 * @date: 2018/11/7 17:11
 * @description:
 */
public class ShopPermissionInterceptor extends HandlerInterceptorAdapter {


    /**
     * 主要做事前的拦截器，急用户操作发生前，改写preHandle里的逻辑，进行哦用户操作
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // session取出
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        @SuppressWarnings("unchecked")
        // session取出 获取当前用户可操作的店铺列表
                List<Shop> shopList = (List<Shop>) request.getSession().getAttribute("shopList");
        //非空判断
        if (currentShop != null && shopList != null) {
            //遍历可操作店铺列表
            for (Shop shop : shopList) {
                //如果当前店铺在可操作的列表里则返回true，进行接下来的用户操作
                if (shop.getShopId().equals(currentShop.getShopId())) {
                    return true;
                }
            }
        }

        //若不满足则返回false 终止操作
        return false;
    }
}
